package com.itheima.leadnews.publish.listener;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmNews;
import com.itheima.leadnews.feign.wemedia.WemediaFeign;
import com.itheima.leadnews.rabbitmq.config.RabbitMqConfig;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class DeadQueueListener {

    @Autowired
    private WemediaFeign wemediaFeign;

    @RabbitListener(queues = RabbitMqConfig.DEAD_LETTER_QUEUE_NAME)
    public void receiveMessage(String wmNewsId) {
        ResponseResult<WmNews> result = wemediaFeign.one(Integer.parseInt(wmNewsId));
        WmNews wmNews = result.getData();
        if (wmNews.getStatus() == WmNews.Status.SUCCESS.getCode() && wmNews.getPublishTime().before(new Date())) {
            wemediaFeign.publish(wmNews);
        }
    }
}
